package cn.quevo.ucenter.client.controller;

import cn.quevo.core.file.ImagesUtil;
import cn.quevo.core.file.entity.WriteImage;
import cn.quevo.core.file.enums.ImageCodeType;
import cn.quevo.core.lang.StringUtils;
import cn.quevo.spring.SpringController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 图形验证码
 *
 * @author July july_sky@foxmail.com
 * @version 1.0
 * @date 2018/4/2 下午6:18
 * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
 */
@Controller("captchaController")
public class CaptchaController extends SpringController {
    private final static Logger log = LoggerFactory.getLogger(CaptchaController.class);

    /**
     * 验证码
     *
     * @param request
     * @param response
     * @param length   字符数量
     * @param width    宽度
     * @param height   高度
     * @author July july_sky@foxmail.com
     * @date 2018-04-02 18:27:11
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    @RequestMapping("/captcha/{length}-{width}-{height}.image")
    public void createImgCode(HttpServletRequest request, HttpServletResponse response,
      @PathVariable("length") int length, @PathVariable("width") int width, @PathVariable("height") int height) {
        String code = StringUtils.getRandomStr(length);
        request.getSession().setAttribute("_captcha", code);
        try {
            WriteImage writeImage = new WriteImage();
            writeImage.setText(code);
            writeImage.setWidth(width);
            writeImage.setHeight(height);
            writeImage.setImageCodeType(ImageCodeType.RANDOM_LINE);
            ImageIO.write(ImagesUtil.createBufferdImage(writeImage), "PNG", response.getOutputStream());
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 验证码
     *
     * @param request
     * @param response
     * @param length   字符数量
     * @param width    宽度
     * @param height   高度
     * @author July july_sky@foxmail.com
     * @date 2018-04-02 18:27:11
     * @Copyright ©2015-2035 湘豫(北京)科技有限公司. All Rights Reserved.
     * @version 1.0
     */
    @RequestMapping("/captcha/{type}/{length}-{width}-{height}.image")
    public void createImgCode(HttpServletRequest request, HttpServletResponse response, @PathVariable("type") String type,
      @PathVariable("length") int length, @PathVariable("width") int width, @PathVariable("height") int height) {
        String code = StringUtils.getRandomStr(length);
        request.getSession().setAttribute("_captcha", code);
        try {
            WriteImage writeImage = new WriteImage();
            writeImage.setText(code);
            writeImage.setWidth(width);
            writeImage.setHeight(height);
            writeImage.setInterLine((width * height)/5);
            writeImage.setImageCodeType(ImageCodeType.format(type));
            ImageIO.write(ImagesUtil.createBufferdImage(writeImage), "PNG", response.getOutputStream());
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }
    }
}

